package util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Bill Lyu on 5/19/2014.
 */
public class HttpUtils {
    public static BrowserType getBrowserType(Map<String, String[]> headerMap) {
        BrowserType browserType;
        List<String> accept = Arrays.asList(headerMap.get("Accept"));
        List<String> agent = Arrays.asList(headerMap.get("User-Agent"));
        RefererType refererType = RefererType.html;
        String acceptStr = accept.toString();
        if (acceptStr.contains("application/vnd.wap.xhtml+xml")) {
            refererType = RefererType.wap2;
        } else {
            String agentStr = agent.toString();
            if (agentStr.contains("iPod")) {
                refererType = RefererType.iphone;
            } else if (agentStr.contains("iPhone")) {
                refererType = RefererType.iphone;
            } else if (agentStr.contains("Android")) {
                refererType = RefererType.Android;
            } else if (agentStr.contains("IEMobile")) {
                refererType = RefererType.IEMobile;
            } else if (acceptStr.contains("DoCoMo/") || acceptStr.contains("portalmmm/")) {
                refererType = RefererType.imode;
            } else if (acceptStr.contains("text/vnd.wap.wml")) {
                refererType = RefererType.wap;
            } else if (acceptStr.contains("text/html")) {
                refererType = RefererType.html;
            }
        }
        boolean isWap = false;
        switch (refererType) {
            case wap:
            case wap2:
            case imode:
            case html:
            case Android:
            case iphone:
            case IEMobile:
                isWap = true;
                break;
        }
        if (isWap) {
            WirelessProtocolType wirelessProtocolType = WirelessProtocolType.none;
            switch (refererType) {
                case wap:
                    wirelessProtocolType = WirelessProtocolType.wap;
                    break;
                case wap2:
                    wirelessProtocolType = WirelessProtocolType.wap2;
                    break;
                case iphone:
                    wirelessProtocolType = WirelessProtocolType.iphone;
                    break;
                case imode:
                    wirelessProtocolType = WirelessProtocolType.imode;
                    break;
                case IEMobile:
                    wirelessProtocolType = WirelessProtocolType.IEMobile;
                    break;
                case html:
                    wirelessProtocolType = WirelessProtocolType.html;
                    break;
                case Android:
                    wirelessProtocolType = WirelessProtocolType.Android;
                    break;
            }
            switch (wirelessProtocolType) {
                case wap:
                case wap2:
                case imode:
                    browserType = BrowserType.web;
                    break;
                case iphone:
                case Android:
                case IEMobile:
                    browserType = BrowserType.smartphone;
                    break;
                case html:
                    browserType = getBrowserType(headerMap, accept, agent);
                    break;
                default:
                    browserType = BrowserType.web;
                    break;
            }
        } else {
            browserType = getBrowserType(headerMap, accept, agent);
        }
        return browserType;
    }

    private static BrowserType getBrowserType(Map<String, String[]> headerMap, List<String> accept, List<String> agent) {
        BrowserType browserType;
        int mobileBrowserCount = 0;
        Pattern pattern = Pattern.compile("/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i");
        for (String agentValue : agent) {
            Matcher matcher = pattern.matcher(agentValue.toLowerCase());
            if (matcher.matches()) {
                mobileBrowserCount++;
                break;
            }
        }
        if (accept.toString().contains("application/vnd.wap.xhtml+xml")
                || headerMap.containsKey("HTTP_X_WAP_PROFILE") || headerMap.containsKey("HTTP_PROFILE")) {
            mobileBrowserCount++;
        }
        String mobileUa = agent.get(0).substring(0, 4).toLowerCase();
        List<String> mobileAgents = Arrays.asList("w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
                "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno",
                "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
                "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
                "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox",
                "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar",
                "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-",
                "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
                "wapr", "webc", "winw", "winw", "xda", "xda-");
        if (mobileAgents.contains(mobileUa)) {
            mobileBrowserCount++;
        }
        String[] allHttps = headerMap.get("ALL_HTTP");
        if (allHttps != null && allHttps.length > 0) {
            List<String> ahs = Arrays.asList(allHttps);
            if (ahs.toString().toLowerCase().contains("operamini")) {
                mobileBrowserCount++;
            }
        }
        if (agent.toString().toLowerCase().contains("iemobile")) {
            mobileBrowserCount++;
        }
        /*if (agent.toString().toLowerCase().contains("windows")) {
            mobileBrowserCount++;
        }*/
        if (mobileBrowserCount > 0) {
            browserType = BrowserType.mobile;
        } else {
            browserType = BrowserType.web;
        }
        return browserType;
    }
}
